package com.music.server.controller;


import com.music.server.pojo.Consumer;
import com.music.server.service.ConsumerService;
import com.music.server.utils.ResultVo;
import com.music.server.vo.ConsumerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端用户 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-05
 */
@RestController
@RequestMapping("consumer")
public class ConsumerController {


    @Autowired
    private ConsumerService consumerService;

    /**
     * 添加前端用户信息
     * @return
     */
    @PostMapping("save")
    public ResultVo save(@RequestBody Consumer consumer){
        consumer.setCreateTime(new Date());
        consumer.setUpdateTime(new Date());
        boolean save = consumerService.save(consumer);
        if (save) return ResultVo.ok();
        return ResultVo.error();
    }

    /**
     * 修改前端用户信息
     * @return
     */
    @PutMapping("update")
    public ResultVo update(@RequestBody Consumer consumer){
        consumer.setUpdateTime(new Date());
        boolean update = consumerService.updateById(consumer);
        if (update) return ResultVo.ok();
        return ResultVo.error();
    }

    /**
     * 查询所有的前端用户信息
     * @return
     */
    @GetMapping("list")
    public ResultVo list(){
        List<Consumer> list = consumerService.list();
        return ResultVo.ok().data("list",list);
    }

    /**
     * 分页查询所有的前端用户信息
     * @return
     */
    @PostMapping("page/{pageNum}/{size}")
    public ResultVo page(@PathVariable Integer pageNum, @PathVariable Integer size, @RequestBody ConsumerVo consumerVo){

        return ResultVo.ok().data("page",consumerService.page(pageNum,size,consumerVo));
    }

    /**
     * 删除前端用户信息及对应的用户收藏歌单
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public ResultVo delete(@PathVariable Integer id){
        boolean delete = consumerService.deleteById(id);
        if (delete) return ResultVo.ok();
        return ResultVo.error();
    }

}

